<template>
  <el-dialog v-model="visible" :title="!dataForm.biddingRuleId ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
<!--          <el-form-item label="关联的采购项目" prop="purchaseProjectId">-->
<!--        <el-input v-model="dataForm.purchaseProjectId" placeholder="关联的采购项目"></el-input>-->
<!--      </el-form-item>-->
          <el-form-item label="竞价开始时间" prop="biddingStartTime">
<!--        <el-input v-model="dataForm.biddingStartTime" placeholder="竞价开始时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
            <el-date-picker v-model="dataForm.biddingStartTime" type="biddingStartTime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="竞价开始时间"></el-date-picker>
          </el-form-item>
          <el-form-item label="竞价结束时间" prop="biddingEndTime">
            <el-date-picker v-model="dataForm.biddingEndTime" type="biddingEndTime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="竞价结束时间"></el-date-picker>
<!--            <el-input v-model="dataForm.biddingEndTime" placeholder="竞价结束时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <ren-radio-group v-model="dataForm.status" dict-type="is_or_no"></ren-radio-group>
      </el-form-item>
          <el-form-item label="是否多次竞价" prop="isManyBidding">
            <ren-radio-group v-model="dataForm.isManyBidding" dict-type="is_or_no"></ren-radio-group>
      </el-form-item>
          <el-form-item label="是否设置降价幅度" prop="isCutPriceRange">
            <ren-radio-group v-model="dataForm.isCutPriceRange" dict-type="is_or_no"></ren-radio-group>
      </el-form-item>
          <el-form-item   label="降价幅度(元)" prop="cutPriceRange" v-show="dataForm.isCutPriceRange == '1' ? true : false">
            <el-input v-model="dataForm.cutPriceRange" :min="0" type="number" :step="1" placeholder="0" />
      </el-form-item>
          <el-form-item label="是否延迟报价" prop="isDelayOffer">
            <ren-radio-group v-model="dataForm.isDelayOffer" dict-type="is_or_no"></ren-radio-group>
      </el-form-item>
          <el-form-item label="延迟时间(分钟)" prop="delayTime" v-show="dataForm.isDelayOffer == '1' ? true : false">
            <el-input-number v-model="dataForm.delayTime" controls-position="right" :min="0" label="0"></el-input-number>
      </el-form-item>
          <el-form-item label="延迟次数" prop="delayNumber" v-show="dataForm.isDelayOffer == '1' ? true : false">
            <el-input-number v-model="dataForm.delayNumber" controls-position="right" :min="0" label="0"></el-input-number>
      </el-form-item>
<!--          <el-form-item label="项目创建人" prop="createUser">-->
<!--        <el-input v-model="dataForm.createUser" placeholder="项目创建人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="项目创建时间，格式：YYYY-MM-DD HH:MM:SS" prop="createTime">-->
<!--        <el-input v-model="dataForm.createTime" placeholder="项目创建时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新人" prop="updateUser">-->
<!--        <el-input v-model="dataForm.updateUser" placeholder="最后更新人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新时间，格式：YYYY-MM-DD HH:MM:SS" prop="updateTime">-->
<!--        <el-input v-model="dataForm.updateTime" placeholder="最后更新时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  biddingRuleId: '',  purchaseProjectId: '', purchaseProjectCode: '',  biddingStartTime: '',  biddingEndTime: '',  isManyBidding: '',  isCutPriceRange: '',  cutPriceRange: '',  isDelayOffer: '',  delayTime: '',  delayNumber: '',  createUser: '',  createTime: '',  updateUser: '',  updateTime: ''});

const rules = ref({
          biddingStartTime: [
      { required: true, message: '竞标开始时间不能为空', trigger: 'blur' }
    ],
          biddingEndTime: [
      { required: true, message: '竞标结束时间不能为空', trigger: 'blur' }
    ],
          status: [
      { required: true, message: '选则是否启用', trigger: 'blur' }
    ],
          cutPriceRange: [
    {
      trigger: "blur",
      pattern: /^[0-9]+(.[0-9]{1,2})?$/, //大于0保留两位
      message: "输入的值必须为正数且保留两位小数"
    },
    {
      trigger: "blur",
      pattern: /^\d{1,9}$|^\d{1,9}[.]\d{1,2}$/, //大于0保留两位
      message: "输入的值最大为999999999.99且最多两位小数"
    }
    ]
  });

const init = (biddingRuleId?: number) => {
  visible.value = true;
  dataForm.biddingRuleId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (biddingRuleId) {
    getInfo(biddingRuleId);
  }
};

// 获取信息
const getInfo = (biddingRuleId: number) => {
  baseService.get("/biddingrule/biddingrule/" + biddingRuleId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.biddingRuleId ? baseService.post : baseService.put)("/biddingrule/biddingrule", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init,
  dataForm
});

const validateConfirmPassword = (rule: any, value: string, callback: (e?: Error) => any): any => {
  if (!dataForm.id && !/\S/.test(value)) {
    return callback(new Error("必填项不能为空"));
  }
  if (dataForm.password !== value) {
    return callback(new Error("确认密码与密码输入不一致"));
  }
  callback();
};
</script>
